package com.atguigu.gmall.flume.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JSONUtil {
    /**
     * 通过异常捕捉判断是否合格的json字符串
     *
     * @param body 待判断的json字符串
     * @return 是json字符串返回true，不是json字符串返回false
     */
    public static boolean isJSONValidate(String body) {
        //copilot begin
        try {
            JSONObject.parseObject(body);
            return true;
        } catch (Exception e) {
            return false;
        }
        //copilot end
    }


    public static void main(String[] args) {
        //copilot begin
        // 将 "aa", "bb", "cc" 存入到List<string> 中
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");

        // 遍历List<string>
        for (String s : list) {
            System.out.println(s);
        }

        Iterator<String> eventIterator = list.iterator();
        while (eventIterator.hasNext()) {
            String event = eventIterator.next();
            if (event.equals("bb")) {
                eventIterator.remove();
            }
        }

        System.out.println(list);


        System.out.println(JSONObject.parseObject("{id: 1}"));
        System.out.println(JSONObject.parseObject("{id: 1, sb}"));
    }


}
